// sub_minsheng_sjts/sjts_selectManager/sjts_selectManager.js
import Toast from '@vant/weapp/toast/toast';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    clickNum: 0,
    resNum: 1,
    pageNum: 1,
    repeat: 1,
    roleType: 3,
    searchValue: '',
    radio: '',
    dataList: []
  },
  async confirm() {
    console.log('点击')
    console.log(this.data.radio)
    if (this.data.radio == '') {
      wx.showToast({
        title: `请勾选${this.data.roleType == 3 ? '外呼员' : '客户经理'}`,
        icon: 'none'
      })
      return
    }
    let corpCreditList = wx.getStorageSync('BusinessPushCheckResult')
    corpCreditList.forEach(element => {
      element.batchNum = wx.getStorageSync('BusinessPushBatchNum')
      element.batchType = wx.getStorageSync('BusinessPushBatchType')
      element.batchTime = wx.getStorageSync('BusinessPushBatchTime')
      element.commissionerID = this.data.radio
    });
    this.data.clickNum = this.data.clickNum + 1
    if (this.data.resNum == this.data.clickNum) {
      let res = {}
      if (this.data.roleType == 3) {
        console.log(corpCreditList,'外呼员')
        res = await getApp().globalData.request(
          "/businessPush/manageBatchBusinessToCaller", corpCreditList,
          "POST"
        )

      } else {
        res = await getApp().globalData.request(
          "/businessPush/manageBatchBusinessToManager", corpCreditList,
          "POST"
        )
      
      }
  

      if (res.code == 200) {
        this.data.resNum = this.data.resNum + 1
        console.log(res, 'listlist')
        wx.showToast({
          title: `流转成功`,
          icon: 'none'
        })
        wx.navigateBack({
          delta: 1, // 返回的页面数，如果 delta 大于现有页面数，则返回到首页。  
          success: function () {
            // 成功返回上一级页面后，立即重新打开该页面实现刷新效果  
            wx.redirectTo({
              url: 'sub_minsheng_sjts/sjts_batch/sjts_batch' // 上级页面的路径  
            });
          }
        });
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'none'
        })
      }
    }


  },
  async getList() {
    console.log(this.data.roleType, '到这里了')
    let params = {
      roleType: this.data.roleType,
    }
    console.log(params, 'paramsparams')
    const res = await getApp().globalData.request(
      "/businessPush/getBatchCommissioner", params,
      "POST"
    )
    console.log(params, res, '接口返回的值')
    let list = res.data
    list.forEach(element => {
      element.checkState = false
      this.data.dataList.push(element)
    });
    this.setData({
      dataList: this.data.dataList,
    })
    this.data.pageNum = this.data.pageNum + 1
  },
  scrollBottom() {
    console.log('到底了')
    if (this.data.repeat == this.data.pageNum) {
      this.data.repeat = this.data.repeat + 1
      this.getList()
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onChange(event) {
    this.setData({
      radio: event.detail,
    });
  },
  onLoad(options) {
    wx.setNavigationBarTitle({
      title:options.roleType==3?'选择外呼员':'选择客户经理' , // 页面标题为传递的标题或默认标题
      success: function () {
        // 设置成功的回调函数
        console.log('标题设置成功');
      },
      fail: function () {
        // 设置失败的回调函数
        console.log('标题设置失败');
      }
    });
    console.log(options, 'options')
    this.setData({
      roleType: options.roleType
    })
    this.scrollBottom()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})